<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ReviewController extends BaseController
{

    public function list(Request $request)
    {
        $pageNum  = $request->get("page_num", 1);
        $pageSize = $request->get("page_size",30);

        $start_time = $request->get("start_time");
        $end_time   = $request->get("end_time");
        $room_num   = $request->get("room_num");

        $where = [['deleted_time' , 0]];
        if ($start_time) {
            $where[] = ['review.created_at', '>=', $start_time];
        }
        if ($end_time) {
            $where[] = ['review.created_at', '<=', $end_time];
        }
        if ($room_num) {
            $where[] = ['room_check_in.room_num', $room_num];
        }

        $model = DB::table('review')
            ->leftJoin('room_check_in', 'review.room_check_in_id', '=', 'room_check_in.id')
            ->where($where)
            ->orderBy('review.id', 'DESC')
            ->paginate($pageSize,
                ['review.*','room_check_in.room_num','room_check_in.user_name','room_check_in.user_phone','room_check_in.in_date','room_check_in.out_date','room_check_in.status'],
                "page",
                $pageNum);

        $list = $model->getCollection();
        return Success(10100, [
            "data"      => $list,
            "page_info" => [
                'current' => $pageNum, 'page_size' => $pageSize, "count" => $model->total(),
                "pages"   => $model->lastPage()
            ]
        ]);
    }

}
